import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/login.vue'
// import HomePage from '../components/HomePage.vue'
// import WelcomePage from '../components/WelcomePage.vue'
// import UserPage from '../components/User/UserPage.vue'
// import RightsPage from '../components/Power/RightsPage'
// import RolesPage from '../components/Power/RolesPage'
// import CatePage from '../components/Goods/CatePage'
// import ParamsPage from '../components/Goods/ParamsPage'
// import GoodsList from '../components/Goods/GoodsList'
// import AddGoods from '../components/Goods/AddGoods'
// import OrderList from '../components/Order/OrderList'
// import ReportPage from '../components/Reports/ReportPage'
// 路由懒加载：分组名称一样则会打包到同一个js文件中 这样一个组件加载了其他的也会加载
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/login.vue')
const HomePage = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/HomePage.vue')
const WelcomePage = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/WelcomePage.vue')

const UserPage = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/User/UserPage.vue')
const RightsPage = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/Power/RightsPage')
const RolesPage = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/Power/RolesPage')

const CatePage = () => import(/* webpackChunkName: "Cate_Params" */ '../components/Goods/CatePage')
const ParamsPage = () => import(/* webpackChunkName: "Cate_Params" */ '../components/Goods/ParamsPage')

const GoodsList = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/Goods/GoodsList')
const AddGoods = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/Goods/AddGoods')

const OrderList = () => import(/* webpackChunkName: "Order_Report" */ '../components/Order/OrderList')
const ReportPage = () => import(/* webpackChunkName: "Order_Report" */ '../components/Reports/ReportPage')

Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: HomePage,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: WelcomePage
      },
      {
        path: '/users',
        component: UserPage
      },
      {
        path: '/rights',
        component: RightsPage
      },
      {
        path: '/roles',
        component: RolesPage
      },
      {
        path: '/categories',
        component: CatePage
      },
      {
        path: '/params',
        component: ParamsPage
      },
      {
        path: '/goods',
        component: GoodsList
      },
      {
        path: '/goods/add',
        component: AddGoods
      },
      {
        path: '/orders',
        component: OrderList
      },
      {
        path: '/reports',
        component: ReportPage
      }
    ]
  }
]
const router = new VueRouter({
  routes
})
// 设置全局路由前置守卫
router.beforeEach((to, from, next) => {
  // 判断地址是不是login，是的话放行
  if (to.path === '/login') return next()
  // 如果地址不是login ，那么需要判断浏览器中是否含有token
  const tokenStr = sessionStorage.getItem('token')
  // 如果没有token字段那么需要强制跳转到login页面进行登录
  if (!tokenStr) return next('/login')
  // 如果有，就继续
  next()
})
export default router
